import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/login',
    name: 'login',
    hidden: true,
    component: () => import(/*webpackChunkName: 'login'*/ '@/pages/Login.vue')
  },
  {
    path: '',
    name: 'index',
    redirect: 'dashboard',
    component: () => import(/*webpackChunkName: 'index'*/ '@/pages/Index.vue'),
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import(/*webpackChunkName: 'dashboard'*/ '@/pages/Dashboard.vue')
      },
      {
        path: '/about',
        name: 'about',
        component: () => import(/*webpackChunkName: 'about'*/ '@/pages/AboutMe.vue')
      },
    ]
  },

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((from, to, next) => {
  if (from.path === '/login') {
    next()
  } else {
    if (!localStorage.getItem(process.env.VUE_APP_TOKEN)) {
      ElMessage.error('请先登录！')
      next('/login')
    } else {
      next()
    }
  }
})

export default router
